<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>LocalStorage</title>
</head>

<body>
  <h2>LocalStorage</h2>
  <button onclick="saveData()">点击在LocalStorage添加数据</button><br><br>
  <button onclick="deleteData()">点击在LocalStorage删除数据</button><br><br>
  <button onclick="getData()">点击在LocalStorage获取数据</button><br><br>
  <button onclick="clearData()">点击清空LocalStorage数据</button><br><br>
</body>
<script type='text/javascript'>
  function saveData () {
    localStorage.setItem('msg1', 'hello');
    localStorage.setItem('msg2', 666);   //只能存储字符串，如果传入非字符串，自动转换成字符串
    localStorage.setItem('msg3', JSON.stringify({ name: '张三', age: 18 }));

  }
  function deleteData () {
    localStorage.removeItem('msg1');
  }
  function getData () {
    const a = localStorage.getItem('msg3');
    console.log(JSON.parse(a));
  }
  function clearData () {
    localStorage.clear()
  }
</script>

</html>